{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 9.2.1 数据预处理 svm_waterimg.py\n",
    "# 1 图像切割\n",
    "# 2 特征提取\n",
    "# 直方图法、颜色矩\n",
    "# 9.2.2 模型构建\n",
    "### 1 模型输入 # 80% 做训练集、20% 做测试集\n",
    "\n",
    "# 数据抽样代码\n",
    "# -*- coding:utf-8 -*-\n",
    "\n",
    "import pandas as pd\n",
    "from pandas import DataFrame,Series\n",
    "import random \n",
    "import numpy as np\n",
    "\n",
    "inputfile = 'moment.csv'\n",
    "data = pd.read_csv(inputfile, encoding='gbk')\n",
    "# data = data.as_matrix()\n",
    "\n",
    "# from random import shuffle # 随机函数\n",
    "# shuffle(data)# 随机打乱数据\n",
    "sampler = np.random.permutation(len(data))\n",
    "d = data.take(sampler).as_matrix()\n",
    "\n",
    "data_train = d[:int(0.8*len(data)),:] #选取前80%做训练集\n",
    "data_test = d[int(0.8*len(data)):,:] #选取后20%做测试集\n",
    "\n",
    "### 2 构建支持向量机模型代码\n",
    "x_train = data_train[:, 2:]*30 #放大特征\n",
    "y_train = data_train[:,0].astype(int)\n",
    "x_test = data_test[:, 2:]*30 #放大特征\n",
    "y_test = data_test[:,0].astype(int)\n",
    "\n",
    "# 导入模型相关的支持向量机函数  建立并且训练模型\n",
    "from sklearn import svm \n",
    "model = svm.SVC()\n",
    "model.fit(x_train, y_train)\n",
    "import pickle\n",
    "pickle.dump(model, open('svcmodel.model','wb'))# model = pickle.load(open('svcmodel.model','rb'))\n",
    "\n",
    "# 导入输出相关的库，生成混淆矩阵\n",
    "from sklearn import metrics\n",
    "cm_train = metrics.confusion_matrix(y_train, model.predict(x_train)) # 训练样本的混淆矩阵\n",
    "\n",
    "cm_test = metrics.confusion_matrix(y_test, model.predict(x_test)) # 测试样本的混淆矩阵\n",
    "\n",
    "df1 = DataFrame(cm_train, index = range(1,6), columns=range(1,6))\n",
    "df2 = DataFrame(cm_test, index = range(1,6), columns=range(1,6))\n",
    "df1.to_excel('outputfile1.xlsx')\n",
    "df2.to_excel('outputfile2.xlsx')\n",
    "model.score(x_train,y_train) # 评价模型训练的准确率\n",
    "model.score(x_test,y_test) # 评价模型测试的准确率\n",
    "\n",
    "### 3 画混淆矩阵图\n",
    "import matplotlib.pyplot as plt #导入作图库\n",
    "% matplotlib inline\n",
    "plt.matshow(cm_train, cmap=plt.cm.Greens) #画混淆矩阵图，配色风格使用cm.Greens，更多风格请参考官网。\n",
    "plt.colorbar() #颜色标签\n",
    "  \n",
    "for x in range(len(cm_train)): #数据标签\n",
    "    for y in range(len(cm_train)):\n",
    "        plt.annotate(cm_train[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center')\n",
    "\n",
    "plt.ylabel('True label') #坐标轴标签\n",
    "plt.xlabel('Predicted label') #坐标轴标签\n",
    "plt.show()\n",
    "\n",
    "'''\n",
    "等价于下面这段\n",
    "from cm_plot import *\n",
    "cm_plot(y_train, model.predict(x_train)).show() # cm_plot是自定义的画混淆矩阵的函数\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据抽样代码\n",
    "# -*- coding:utf-8 -*-\n",
    "\n",
    "import pandas as pd\n",
    "from pandas import DataFrame,Series\n",
    "import random \n",
    "import numpy as np\n",
    "\n",
    "inputfile = 'moment.csv'\n",
    "data = pd.read_csv(inputfile, encoding='gbk')\n",
    "# data = data.as_matrix()\n",
    "\n",
    "# from random import shuffle # 随机函数\n",
    "# shuffle(data)# 随机打乱数据\n",
    "sampler = np.random.permutation(len(data))\n",
    "d = data.take(sampler).as_matrix()\n",
    "\n",
    "data_train = d[:int(0.8*len(data)),:] #选取前80%做训练集\n",
    "data_test = d[int(0.8*len(data)):,:] #选取后20%做测试集\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.95679012345679015"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构建支持向量机模型代码\n",
    "x_train = data_train[:, 2:]*30 #放大特征\n",
    "y_train = data_train[:,0].astype(int)\n",
    "x_test = data_test[:, 2:]*30 #放大特征\n",
    "y_test = data_test[:,0].astype(int)\n",
    "\n",
    "# 导入模型相关的支持向量机函数  建立并且训练模型\n",
    "from sklearn import svm \n",
    "model = svm.SVC()\n",
    "model.fit(x_train, y_train)\n",
    "import pickle\n",
    "pickle.dump(model, open('svcmodel.model','wb'))# model = pickle.load(open('svcmodel.model','rb'))\n",
    "\n",
    "# 导入输出相关的库，生成混淆矩阵\n",
    "from sklearn import metrics\n",
    "cm_train = metrics.confusion_matrix(y_train, model.predict(x_train)) # 训练样本的混淆矩阵\n",
    "\n",
    "cm_test = metrics.confusion_matrix(y_test, model.predict(x_test)) # 测试样本的混淆矩阵\n",
    "\n",
    "df1 = DataFrame(cm_train, index = range(1,6), columns=range(1,6))\n",
    "df2 = DataFrame(cm_test, index = range(1,6), columns=range(1,6))\n",
    "df1.to_excel('outputfile1.xlsx')\n",
    "df2.to_excel('outputfile2.xlsx')\n",
    "model.score(x_train,y_train) # 评价模型训练的准确率\n",
    "model.score(x_test,y_test) # 评价模型测试的准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAAD3CAYAAAD/jPo0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8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8D4yR9CkDSgZKOBF4C+ksaFJy3v7n8\nlgGXBO/NldQT2MHeCwkvBr7WoO+iSNKhwGPAqZK6SepO8rIlle5AjaQuwFf3+dgZknKCmAcC/wjq\nviQ4H0lHSjowRD2dhkJuUfIWQ8zMbEvwl/duSQcEh39kZi9LughYKOl9kpciTa0afiUwK5gNOAFc\nYmZPSXpCyYVN/zfoZxgKPBW0WHYC55jZKkn3AGtJrjWwIkTI/wosB7YE/zeM6XXgGaAHcLGZfSjp\nDpJ9D6uUrHwLcGq4r05nkX0thkiWqHPONW102Wh79On/C3Vuz/zeoZaoSwe/lHDONeKXEs7FKNl/\nkH1/nz0xOBez7Oth8MTgXOx8HINzbh/ZOYNT9l3cONfJpHMcQ/B8zT8krW/uCdxUPDE4F7v0pAZJ\nucDtJJ/LKQHOklTSmog8MTgXJ6V1PoZjgPVm9qqZ1QJ/A6a2JixPDM7FKM1PVxYBmxvsVwbHWsw7\nH52L0aqVqxd3yzuwT8jTu0qqaLA/K1OzTnlicC5GZjYhjcVVAf0a7BcHx1rMLyWc6zhWAIMlDZCU\nT3ISnjBzbDTiLQbnOggz2y3pmyQfdc8F/mhmL7SmLH+60jnXiF9KOOca8cTgnGvEE4NzrhFPDM65\nRjwxOOca8cTgnGvEE4NzrhFPDM65Rv4/TYEdWGJMr88AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0xdd85128>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\"\\nu'\\xe7\\xad\\x89\\xe4\\xbb\\xb7\\xe4\\xba\\x8e\\xe4\\xb8\\x8b\\xe9\\x9d\\xa2\\xe8\\xbf\\x99\\xe6\\xae\\xb5\\nfrom cm_plot import *\\ncm_plot(y_train, model.predict(x_train)).show() # cm_plot\\xe6\\x98\\xaf\\xe8\\x87\\xaa\\xe5\\xae\\x9a\\xe4\\xb9\\x89\\xe7\\x9a\\x84\\xe7\\x94\\xbb\\xe6\\xb7\\xb7\\xe6\\xb7\\x86\\xe7\\x9f\\xa9\\xe9\\x98\\xb5\\xe7\\x9a\\x84\\xe5\\x87\\xbd\\xe6\\x95\\xb0'\\n\""
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt #导入作图库\n",
    "% matplotlib inline\n",
    "plt.matshow(cm_train, cmap=plt.cm.Greens) #画混淆矩阵图，配色风格使用cm.Greens，更多风格请参考官网。\n",
    "plt.colorbar() #颜色标签\n",
    "  \n",
    "for x in range(len(cm_train)): #数据标签\n",
    "    for y in range(len(cm_train)):\n",
    "        plt.annotate(cm_train[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center')\n",
    "\n",
    "plt.ylabel('True label') #坐标轴标签\n",
    "plt.xlabel('Predicted label') #坐标轴标签\n",
    "plt.show()\n",
    "\n",
    "'''\n",
    "等价于下面这段\n",
    "from cm_plot import *\n",
    "cm_plot(y_train, model.predict(x_train)).show() # cm_plot是自定义的画混淆矩阵的函数\n",
    "'''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
